package com.bank.dao;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bank.entity.TransAccountList;

@Service("transAccountListDao")
public class TransAccountListDaoImpl  implements TransAccountListDao{

	Logger logger = Logger.getLogger(TransAccountListDaoImpl.class);
	
	@Autowired
	SessionFactory sessionFactory;
	
	public void save(TransAccountList list) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		session.save(list);
		session.flush();
	}
	@SuppressWarnings("unchecked") 
	public List<Object> getAccountDetailList(Map<String, Object> params){
		String sql = "from TransAccountList where 1=1 ";
		Integer offsetInt  = (Integer)params.get("offset");
		Integer limitInt = (Integer)params.get("limit");
		String trans_workdate = (String)params.get("trans_workdate");
		String teller_serno  = (String)params.get("teller_serno");
		String account_no = (String)params.get("account_no");
		String customer_no = (String)params.get("customer_no");
		if(trans_workdate!=null && trans_workdate.equals("")==false){
			sql = sql+" and trans_workdate ='"+trans_workdate+"'";
		}
		if(teller_serno != null && teller_serno.equals("")==false){
			sql = sql+" and  teller_serno='"+teller_serno+"'";
		}
		if(account_no != null && account_no.equals("")==false){
			sql = sql+"  and account_no='"+account_no+"'";
		}
		if(customer_no != null && customer_no.equals("")==false){
			sql = sql + " and customer_no='"+customer_no+"'";
		}
		Session  session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(sql);
		query.setFirstResult(offsetInt);
		query.setMaxResults(limitInt);
		List<Object> accounDeatiltList = query.list();	
		return accounDeatiltList;
	}
	public int getAccounDetailtListCount(Map<String, Object> params){
		String sqlCount = "select count(*) from TransAccountList where 1=1 ";
		String trans_workdate = (String)params.get("trans_workdate");
		String teller_serno  = (String)params.get("teller_serno");
		String account_no = (String)params.get("account_no");
		String customer_no = (String)params.get("customer_no");
		String trans_code = (String)params.get("trans_code");   // 交易码
		if(trans_workdate!=null && trans_workdate.equals("")==false){
			sqlCount = sqlCount+" and  customer_number ='"+trans_workdate+"'";
		}
		if(teller_serno != null && teller_serno.equals("")==false){
			sqlCount = sqlCount+" and teller_serno='"+teller_serno+"'";
		}
		if(account_no != null && account_no.equals("")==false){
			sqlCount = sqlCount+" and account_no='"+account_no+"'";
		}
		if(customer_no != null && customer_no.equals("")==false){
			sqlCount = sqlCount + " and customer_name='"+customer_no+"'";
		}
		if(trans_code != null && trans_code.equals("")==false){
			sqlCount = sqlCount + " and trans_code='"+trans_code+"'";
		}
		Session  session = sessionFactory.getCurrentSession();
		int totalRecord = Integer.parseInt(session.createQuery(sqlCount).uniqueResult().toString());
		logger.debug("totalRecord"+totalRecord);
		return totalRecord;

	}
	
}
